<template>
  <div class="detail-page">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back-btn" @click="goBack">
        <i class="arrow-left">←</i>
      </div>
      <div class="title">消息详情</div>
    </div>

    <!-- 消息内容 -->
    <div class="message-detail" v-if="!loading">
      <div v-for="(item, index) in messageDetail"
           :key="index"
           class="message-item">
        <div class="message-header">
          <div class="message-title">群发内容</div>
        </div>
        <div class="message-time">{{ item.creathTime }}</div>
        <div class="message-content">
          {{ item.messageContent }}
        </div>

        <!-- 接收人列表 -->
        <div class="receivers-section">
          <div class="receivers-title">
            接收人 ({{ item.userList ? item.userList.length : 0 }}人)
          </div>
          <div class="receivers-list">
            <div v-for="(user, rIndex) in item.userList"
                 :key="rIndex"
                 class="receiver-item">
              <img src="@/assets/images/屏幕截图 2025-03-01 110826.png" class="avatar" alt="头像">
              <span class="name">{{ user.userName }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="loading">
      加载中...
    </div>
  </div>
</template>

<script>
import { selectMessageDetails } from "@/api/message/message";

export default {
  name: 'MessageDetail',
  data() {
    return {
      messageDetail: [],  // 改为数组
      loading: false
    }
  },
  created() {
    this.getMessageDetail();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    async getMessageDetail() {
      try {
        const id = parseInt(this.$route.query.id, 10);
        if (!id || isNaN(id)) {
          this.$message.error('无效的消息ID');
          return;
        }

        this.loading = true;
        const response = await selectMessageDetails(id);
        if (response.code === 200) {
          this.messageDetail = response.data || [];  // 确保是数组
        } else {
          this.$message.error(response.msg || '获取消息详情失败');
        }
      } catch (error) {
        console.error('获取消息详情失败:', error);
        this.$message.error('获取消息详情失败，请稍后重试');
      } finally {
        this.loading = false;
      }
    },
    goToSelectDisease() {
      this.$router.push('/message/select-disease')
    }
  }
}
</script>

<style scoped lang="scss">
.detail-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.nav-header {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #eee;

  .back-btn {
    font-size: 20px;
    color: #333;
    margin-right: 16px;
    cursor: pointer;
  }

  .title {
    font-size: 18px;
    font-weight: 500;
  }
}

.message-detail {
  margin: 16px;
}

.message-item {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 16px;

  .message-header {
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .message-title {
      font-size: 16px;
      font-weight: 500;
      color: #333;
    }
  }

  .message-time {
    font-size: 14px;
    color: #999;
    margin-bottom: 16px;
  }

  .message-content {
    font-size: 14px;
    line-height: 1.6;
    color: #333;
    white-space: pre-wrap;
    margin-bottom: 24px;
  }
}

.receivers-section {
  .receivers-title {
    font-size: 14px;
    color: #666;
    margin-bottom: 16px;
    cursor: pointer;
  }

  .receivers-list {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;

    .receiver-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 8px;

      .avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background-color: #409EFF;
      }

      .name {
        font-size: 12px;
        color: #666;
      }
    }
  }
}

.loading {
  text-align: center;
  color: #999;
  padding: 32px 0;
}
</style>
